﻿using Publicuse.Entity;
using Publicuse.Util;
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;

namespace Publicuse.Export
{
    public class QuartzExport
    {
        /// <summary>
        /// 系统任务调度导出数据设置
        /// </summary>
        /// <param name="excelName"></param>
        /// <param name="fileName"></param>
        /// <param name="list"></param>
        /// <param name="msg"></param>
        /// <returns></returns>
        public static MemoryStream ExportData(string fileName, string excelName, List<QuzrtTaskOptions> list, out string msg)
        {
            msg = string.Empty;
            MemoryStream ms = ExcelUtil.GetExcelUtil.InitExportData(fileName, excelName, list, (row, cell, sheet1, TextSytle, srow, hssfworkbook) => {

                foreach (QuzrtTaskOptions item in list)
                {
                    srow++;
                    row = sheet1.CreateRow(srow);

                    //作业名称
                    cell = row.CreateCell(0);
                    cell.CellStyle = TextSytle;
                    cell.SetCellValue(item.GroupName);

                    //作业分组
                    cell = row.CreateCell(1);
                    cell.CellStyle = TextSytle;
                    cell.SetCellValue(item.GroupName);

                    //最后执行时间
                    cell = row.CreateCell(2);
                    cell.CellStyle = TextSytle;
                    cell.SetCellValue(item.LastRunTime?.ToString("yyyy-MM-dd HH:mm:ss"));

                    //间隔(Cron)
                    cell = row.CreateCell(3);
                    cell.CellStyle = TextSytle;
                    cell.SetCellValue(item.Intervale);

                    //请求方式
                    cell = row.CreateCell(4);
                    cell.CellStyle = TextSytle;
                    cell.SetCellValue(item.RequestType);

                    var status = string.Empty;
                    switch (item.status)
                    {
                        case 0:
                            status = "正常";
                            break;
                        case 1:
                            status = "暂停";
                            break;
                        case 2:
                            status = "完成";
                            break;
                        case 3:
                            status = "异常";
                            break;
                        case 4:
                            status = "阻塞";
                            break;
                        case 5:
                            status = "停止";
                            break;
                        default:
                            status = "不存在";
                            break;
                    }
                    //状态
                    cell = row.CreateCell(5);
                    cell.CellStyle = TextSytle;
                    cell.SetCellValue(status);

                    //作业地址
                    cell = row.CreateCell(6);
                    cell.CellStyle = TextSytle;
                    cell.SetCellValue(item.ApiUrl);

                    //请求参数
                    cell = row.CreateCell(7);
                    cell.CellStyle = TextSytle;
                    cell.SetCellValue(item.ParamData);

                    //请求头
                    cell = row.CreateCell(8);
                    cell.CellStyle = TextSytle;
                    cell.SetCellValue(item.HeaderData);

                    //描述
                    cell = row.CreateCell(9);
                    cell.CellStyle = TextSytle;
                    cell.SetCellValue(item.Remark);
                }
            }, out msg, 2);
            return ms;
        }
    }
}
